package com.v1.video.statistics;

import android.content.Context;

import com.v1.video.AppContext;
import com.v1.video.Constant;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.RequestData;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.ParamList;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.util.Utils;

import java.util.Map;


/**
 * Created by jxj42 on 2016/12/22.
 */

public class CustomerStatistics {
    /**
     * 进入app应用时的时间
     */
    private static final String ENTER_APP_TIME = "enterAppTime";
    /**
     * 退出app应用的时间
     */
    private static final String EXIT_APP_TIME = "exitAppTime";
    /**
     * in end间隔 30s
     */
    private static final int IN_END_INTERVAL = 1;

    public static void enterApp(Context context) {
    	AppContext app = AppContext.getInstance();
        if (app.getAttribute(CustomerStatistics.EXIT_APP_TIME) == null) {
            sendStartApp(context);
        } else {
            long time = (System.currentTimeMillis() / 1000) - Long.parseLong(app.getAttribute(EXIT_APP_TIME).toString());
            //本次打开应用距离上次退出应用的时间超过30秒才发送in统计
            if (time > IN_END_INTERVAL) {
                sendStartApp(context);
                app.setAttribute(EXIT_APP_TIME, System.currentTimeMillis() / 1000);
            }
        }

        //记录进入应用时的时间
        if (app.getAttribute(CustomerStatistics.ENTER_APP_TIME) == null) {
            app.setAttribute(CustomerStatistics.ENTER_APP_TIME, System.currentTimeMillis() / 1000);
        }
    }

    public static void exitApp() {
    	AppContext app = AppContext.getInstance();
        //记录退出应用时的时间
        app.setAttribute(EXIT_APP_TIME, System.currentTimeMillis() / 1000);
    }

    /**
     * 开机上报统计
     */
    private static void sendStartApp(Context context) {
        String uid = LoginInfo.getInstance().isLogin() ? LoginInfo.getInstance().getUserId() : "0";
        String url = String.format(V1VideoHttpApi.REPORT_DEVICE(), Constant.DEVICEID);
        ParamList parameters = new ParamList();
        parameters.add(new ParamList.Parameter("user_id", uid));
        parameters.add(new ParamList.Parameter("mac", Utils.getMacAddress(context)));
        Map<String, String> map = Utils.getLongitudeAndLatitude(context);
		if (map != null) {
			parameters.add(new ParamList.Parameter("longitude", map.get("longitude")));
	        parameters.add(new ParamList.Parameter("latitude", map.get("latitude")));
		} else {
			parameters.add(new ParamList.Parameter("longitude", ""));
	        parameters.add(new ParamList.Parameter("latitude", ""));
		}
        parameters.add(new ParamList.Parameter("vipExpires", ""));
        parameters.add(new ParamList.Parameter("baseStation", ""));
        parameters.add(new ParamList.Parameter("cellIdentity", ""));
        parameters.add(new ParamList.Parameter("IMEI", Constant.DEVICEID));
        parameters.add(new ParamList.Parameter("IMSI", Utils.getSubscriberId(context)));
        parameters.add(new ParamList.Parameter("cateid", "1"));
        RequestManager.getInstance().postRequest(context, url, parameters, RequestData.class, new RequestManager.OnResponseListener() {
            @Override
            public void onSuccess(Object object, int code, String message,String keyId) {
                RequestData requestData = (RequestData) object;
            }

            @Override
            public void onFailure(int code, String message) {

            }
        });
    }
}
